 ********************************************
 * Table 6: Price elasticity by gender
 ********************************************
 * CALCULATE ELASTICITIES OF PRICE BY GENDER
 * The elasticities use the model with gender interaction computed in Table5_Heterogeneity_by_gender.do
 ********************************************

* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {

preserve
set seed  202

replace p_i1 = p_i1*(1.01) if typeprov==`i' 
replace p_i2 = p_i2*(1.01) if typeprov==`i'
replace p_i3 = p_i3*(1.01) if typeprov==`i' 
replace p_i4 = p_i4*(1.01) if typeprov==`i'

replace p_i1_male = p_i1*male if typeprov==`i' 
replace p_i2_male = p_i2*male if typeprov==`i'
replace p_i3_male = p_i3*male if typeprov==`i' 
replace p_i4_male = p_i4*male if typeprov==`i'


replace p_i1_fem = p_i1*female if typeprov==`i' 
replace p_i2_fem = p_i2*female if typeprov==`i'
replace p_i3_fem = p_i3*female if typeprov==`i' 
replace p_i4_fem = p_i4*female if typeprov==`i'

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR0GE)/PR0GE if esample

display "THESE ARE OWN AND CROSS PRICE ELASTICiTIES FOR 1% INCREASE IN PRICE IN TYPEPROVIDER= `i' "

table typeprov female, c(mean el_p`i' med el_p`i' sd el_p`i' semean el_p`i') format(%9.3f) col row

restore 
}


********************************************************************************************
 * Table 6: Calculate the elasticities'  standard errors by bootrapping
********************************************************************************************
** This script bootstraps price elasticities of demand by gender (and the price elasticities of demand by income quantile) in batchs of 20 iterations
** It saves the simulated elasticities in a series of files called mixboot_g4_nu8g_#.dta (where # is 1, 2, 3, etc and indicate a batch)
** Data files mixboot_g4_nu8g_#.dta are then appended. The standard errors is the standard deviations of the empirical distribution of bootstrapped elasticities. 

set seed 20205
local bsize=20 /* size of the iterations*/
global regressors "dist_dw_hf_2 dist_22   nu_8d alt2-alt4 nu_8dsq"
global random "doctor_tot  generator2009 beds p_i1_male p_i2_male p_i3_male p_i4_male  p_i1_fem p_i2_fem p_i3_fem p_i4_fem "
global control "idid choicesit quartile no_facility i1 i2 i3 i4 p_i1 p_i2 p_i3 p_i4 male female age age2 typeprov q01_10 " 
set more off
tempname 3
postfile `3' iter   el_p11 el_p12 el_p13 el_p14  /// *self*
					el_p21 el_p22 el_p23 el_p24  /// *public*
					el_p31 el_p32 el_p33 el_p34  /// *private*
					el_p41 el_p42 el_p43 el_p44  /// *doctor*
					///
					el_pc111 el_pc121 el_pc131 el_pc141 /// *quantile =1*
					el_pc211 el_pc221 el_pc231 el_pc241 ///
				    el_pc311 el_pc321 el_pc331 el_pc341 ///
					el_pc411 el_pc421 el_pc431 el_pc441 ///
					el_pc112 el_pc122 el_pc132 el_pc142 /// *quantile =2*
					el_pc212 el_pc222 el_pc232 el_pc242 ///
				    el_pc312 el_pc322 el_pc332 el_pc342 ///
					el_pc412 el_pc422 el_pc432 el_pc442 ///
					el_pc113 el_pc123 el_pc133 el_pc143 /// *quantile =3*
					el_pc213 el_pc223 el_pc233 el_pc243 ///
				    el_pc313 el_pc323 el_pc333 el_pc343 ///
					el_pc413 el_pc423 el_pc433 el_pc443 ///
					el_pc114 el_pc124 el_pc134 el_pc144 /// *quantile =4*
					el_pc214 el_pc224 el_pc234 el_pc244 ///
				    el_pc314 el_pc324 el_pc334 el_pc344 ///
					el_pc414 el_pc424 el_pc434 el_pc444 ///
					///
					el_p11m el_p12m el_p13m el_p14m  /// *self for male*
					el_p21m el_p22m el_p23m el_p24m  /// *public*
					el_p31m el_p32m el_p33m el_p34m  /// *private*
					el_p41m el_p42m el_p43m el_p44m  /// *doctor*
					el_p11f el_p12f el_p13f el_p14f  /// *self for female *
					el_p21f el_p22f el_p23f el_p24f  /// *public*
					el_p31f el_p32f el_p33f el_p34f  /// *private*
					el_p41f el_p42f el_p43f el_p44f  /// *doctor*
					///
					emed_p11m emed_p12m emed_p13m emed_p14m  /// *MEDIAN self for male*
					emed_p21m emed_p22m emed_p23m emed_p24m  /// *public*
					emed_p31m emed_p32m emed_p33m emed_p34m  /// *private*
					emed_p41m emed_p42m emed_p43m emed_p44m  /// *doctor*
					emed_p11f emed_p12f emed_p13f emed_p14f  /// *MEDIAN self for female *
					emed_p21f emed_p22f emed_p23f emed_p24f  /// *public*
					emed_p31f emed_p32f emed_p33f emed_p34f  /// *private*
					emed_p41f emed_p42f emed_p43f emed_p44f  /// *doctor*
					///
					del_p11 del_p12 del_p13 del_p14  ///
					del_p21 del_p22 del_p23 del_p24  ///
					del_p31 del_p32 del_p33 del_p34  ///
					del_p41 del_p42 del_p43 del_p44  ///
					///
                    using mixboot_g4_nu8g_6, replace   /*name of datafile to be changed manually*/
					
forvalues iters1=1(1)`bsize' {
use tempdata_mixedlogitboot_g, clear
	replace waittime2009 = -waittime2009
	replace nomed2009 = -nomed2009
sum waittime2009 nomed2009 
keep idid no_facility d_ $control $regressors $random
bsample 1000, cluster(idid) idcluster(idnew)
replace choicesit =idnew


				noisily di "  ITER =" "`iters1'" "ESTIMATION OF RANDOM PARAMETER MODEL "
				* estimate RPM for bootstrapped sample 'iter'
			    mixlogit d_  $regressors  , group(choicesit) id(idnew) cluster(q01_10) rand($random) from(b52g, copy) tolerance(0.1)

	            mixlpred PR0, nrep(500)
	            
* CALCULATE  THE SIMULATED ELASTICITY BY CHANGING X BY 1%
* for each type of provider increase the price (for everybody) by 1%
forvalues i=1/4 {
noisily di "WE ARE GOING TO CHANGE PRICE FOR PROVIDER=" "`i'" 

local i=`i'     //*typeprovider whose price is being changed*//
preserve

*set seed 563531
replace p_i1 = p_i1*(1.01) if typeprov==`i' 
replace p_i2 = p_i2*(1.01) if typeprov==`i'
replace p_i3 = p_i3*(1.01) if typeprov==`i' 
replace p_i4 = p_i4*(1.01) if typeprov==`i'

replace p_i1_male = p_i1*male if typeprov==`i' 
replace p_i2_male = p_i2*male if typeprov==`i'
replace p_i3_male = p_i3*male if typeprov==`i' 
replace p_i4_male = p_i4*male if typeprov==`i'

replace p_i1_fem = p_i1*female if typeprov==`i' 
replace p_i2_fem = p_i2*female if typeprov==`i'
replace p_i3_fem = p_i3*female if typeprov==`i' 
replace p_i4_fem = p_i4*female if typeprov==`i'

*list typeprov p_i1 p_i2 p_i3 p_i4  in 1/3  //*to check changes are correct//*

mixlpred PR1`i', nrep(500)

gen el_p`i' = 100*(PR1`i'-PR0)/PR0

* own and cross elasticities: for each provider (whose price changes), store own elasticity and cross
* if price changes in i, what happen to # => E#z(i)

noisily di "===> OWN AND CROSS ELASTICITY: PRICE CHANGE IN PROVIDER=" "`i'" 

qui sum el_p`i' if typeprov==1
local el_p`i'1 = r(mean)
qui sum el_p`i' if typeprov==2
local el_p`i'2 = r(mean)
qui sum el_p`i' if typeprov==3
local el_p`i'3 = r(mean)
qui sum el_p`i' if typeprov==4
local el_p`i'4 = r(mean)

noisily di "===> OWN AND CROSS ELASTICITY FOR MALE: PRICE CHANGE IN PROVIDER=" "`i'" 

qui sum el_p`i' if typeprov==1 & female==0
local el_p`i'1m = r(mean)
qui sum el_p`i' if typeprov==2 & female==0
local el_p`i'2m = r(mean)
qui sum el_p`i' if typeprov==3 & female==0
local el_p`i'3m = r(mean)
qui sum el_p`i' if typeprov==4 & female==0
local el_p`i'4m = r(mean)

noisily di "===> OWN AND CROSS ELASTICITY FOR FEMALE: PRICE CHANGE IN PROVIDER=" "`i'" 

sum el_p`i' if typeprov==1 & female==1
local el_p`i'1f = r(mean)
sum el_p`i' if typeprov==2 & female==1
local el_p`i'2f = r(mean)
sum el_p`i' if typeprov==3 & female==1
local el_p`i'3f = r(mean)
sum el_p`i' if typeprov==4 & female==1
local el_p`i'4f = r(mean)

* generate the difference between elasticity of female and male 
noisily di "===> GET THE DIFFERENCE IN PRICE ELASTICITIES FEMALE-MALE FOR A 1% PRICE CHANGE IN PROVIDER=" "`i'" 

qui ttest el_p`i' if typeprov==1, by(female)
local del_p`i'1 = r(mu_2)-r(mu_1)
qui ttest el_p`i' if typeprov==2, by(female)
local del_p`i'2 = r(mu_2)-r(mu_1)
qui ttest el_p`i' if typeprov==3, by(female)
local del_p`i'3 = r(mu_2)-r(mu_1)
qui ttest el_p`i' if typeprov==4, by(female)
local del_p`i'4 = r(mu_2)-r(mu_1)

* for each income quartile sum elasticity  within group x and store elasticity
* interpretation: price elasticity - changing price of provider i, what happen to provider # and quartile q => E#z(i)

*forvalues q=1/2 {
*sum el_p`i' if typeprov==1 & quartile==`q'
*local el_pc`i'1`q' = r(mean)
*sum el_p`i' if typeprov==2 & quartile==`q'
*local el_pc`i'2`q' = r(mean)
*sum el_p`i' if typeprov==3 & quartile==`q'
*local el_pc`i'3`q' = r(mean)
*sum el_p`i' if typeprov==4 & quartile==`q'
*local el_pc`i'4`q' = r(mean)

foreach  q of numlist 1 2 3 4 {

noisily di "===> OWN AND CROSS ELASTICITY BY INCOME QUANTILE: PRICE CHANGE IN PROVIDER=" "`i'" "  INCOME QUARTILE=" "`q'"

sum el_p`i' if typeprov==1 & quartile==`q'
local el_pc`i'1`q' =r(mean)
sum el_p`i' if typeprov==2 & quartile==`q'
local el_pc`i'2`q' =r(mean)
sum el_p`i' if typeprov==3 & quartile==`q'
local el_pc`i'3`q' =r(mean)
sum el_p`i' if typeprov==4 & quartile==`q'
local el_pc`i'4`q' =r(mean)
}

noisily di "===> OWN AND CROSS MEDIAN ELASTICITY FOR MALE: PRICE CHANGE IN PROVIDER=" "`i'" 

qui sum el_p`i' if typeprov==1 & female==0, det
local emed_p`i'1m = r(p50)
qui sum el_p`i' if typeprov==2 & female==0, det
local emed_p`i'2m = r(p50)
qui sum el_p`i' if typeprov==3 & female==0, det
local emed_p`i'3m = r(p50)
qui sum el_p`i' if typeprov==4 & female==0, det
local emed_p`i'4m = r(p50)

noisily di "===> OWN AND CROSS MEDIAN ELASTICITY FOR FEMALE: PRICE CHANGE IN PROVIDER=" "`i'" 

qui sum el_p`i' if typeprov==1 & female==1, det
local emed_p`i'1f = r(p50)
qui sum el_p`i' if typeprov==2 & female==1, det
local emed_p`i'2f = r(p50)
qui sum el_p`i' if typeprov==3 & female==1, det
local emed_p`i'3f = r(p50)
qui sum el_p`i' if typeprov==4 & female==1, det
local emed_p`i'4f = r(p50)


restore


}

local iter=`iters1'


post `3' (`iter')   (`el_p11') (`el_p12') (`el_p13') (`el_p14')  /// *self*
					(`el_p21') (`el_p22') (`el_p23') (`el_p24')  /// *public*
					(`el_p31') (`el_p32') (`el_p33') (`el_p34')  /// *private*
					(`el_p41') (`el_p42') (`el_p43') (`el_p44')  /// *doctor*
					///
					(`el_pc111') (`el_pc121') (`el_pc131') (`el_pc141') ///  /*quantile ==1*/
					(`el_pc211') (`el_pc221') (`el_pc231') (`el_pc241') ///
				    (`el_pc311') (`el_pc321') (`el_pc331') (`el_pc341') ///
					(`el_pc411') (`el_pc421') (`el_pc431') (`el_pc441') /// 
					(`el_pc112') (`el_pc122') (`el_pc132') (`el_pc142') ///  /*quantile ==2*/
					(`el_pc212') (`el_pc222') (`el_pc232') (`el_pc242') ///
				    (`el_pc312') (`el_pc322') (`el_pc332') (`el_pc342') ///
					(`el_pc412') (`el_pc422') (`el_pc432') (`el_pc442') ///
					(`el_pc113') (`el_pc123') (`el_pc133') (`el_pc143') ///  /*quantile ==3*/
					(`el_pc213') (`el_pc223') (`el_pc233') (`el_pc243') ///
				    (`el_pc313') (`el_pc323') (`el_pc333') (`el_pc343') ///
					(`el_pc413') (`el_pc423') (`el_pc433') (`el_pc443') ///
					(`el_pc114') (`el_pc124') (`el_pc134') (`el_pc144') ///  /*quantile ==4*/
					(`el_pc214') (`el_pc224') (`el_pc234') (`el_pc244') ///
				    (`el_pc314') (`el_pc324') (`el_pc334') (`el_pc344') ///
					(`el_pc414') (`el_pc424') (`el_pc434') (`el_pc444') ///
					///
					(`el_p11m') (`el_p12m') (`el_p13m') (`el_p14m')  /// *self for male*
					(`el_p21m') (`el_p22m') (`el_p23m') (`el_p24m')  /// *public*
					(`el_p31m') (`el_p32m') (`el_p33m') (`el_p34m')  /// *private*
					(`el_p41m') (`el_p42m') (`el_p43m') (`el_p44m')  /// *doctor*
					///
					(`el_p11f') (`el_p12f') (`el_p13f') (`el_p14f')  /// *self for female*
					(`el_p21f') (`el_p22f') (`el_p23f') (`el_p24f')  /// *public*
					(`el_p31f') (`el_p32f') (`el_p33f') (`el_p34f')  /// *private*
					(`el_p41f') (`el_p42f') (`el_p43f') (`el_p44f')  /// *doctor*
					///
					(`emed_p11m') (`emed_p12m') (`emed_p13m') (`emed_p14m')  /// *MEDIAN self for male*
					(`emed_p21m') (`emed_p22m') (`emed_p23m') (`emed_p24m')  /// *public*
					(`emed_p31m') (`emed_p32m') (`emed_p33m') (`emed_p34m')  /// *private*
					(`emed_p41m') (`emed_p42m') (`emed_p43m') (`emed_p44m')  /// *doctor*
					///
					(`emed_p11f') (`emed_p12f') (`emed_p13f') (`emed_p14f')  /// *MEDIAN self for female*
					(`emed_p21f') (`emed_p22f') (`emed_p23f') (`emed_p24f')  /// *public*
					(`emed_p31f') (`emed_p32f') (`emed_p33f') (`emed_p34f')  /// *private*
					(`emed_p41f') (`emed_p42f') (`emed_p43f') (`emed_p44f')  /// *doctor* 
					///
					(`del_p11') (`del_p12') (`del_p13') (`del_p14')  /// difference in elasticities of male and female 
					(`del_p21') (`del_p22') (`del_p23') (`del_p24')  ///
					(`del_p31') (`del_p32') (`del_p33') (`del_p34')  ///
					(`del_p41') (`del_p42') (`del_p43') (`del_p44')  
}
postclose `3'
		

*use     "mixboot_g4_nu8g.dta", clear

use           "mixboot_g4_nu8g_1.dta", clear
append  using "mixboot_g4_nu8g_2.dta"
append  using "mixboot_g4_nu8g_3.dta"
append  using "mixboot_g4_nu8g_4.dta"
append  using "mixboot_g4_nu8g_5.dta"
append  using "mixboot_g4_nu8g_6.dta"
append  using "mixboot_g4_nu8g_7.dta"
append  using "mixboot_g4_nu8g_8.dta"
append  using "mixboot_g4_nu8g_9.dta"
append  using "mixboot_g4_nu8g_10.dta"

	
label var el_p11 "demand elast of self, 1% price change in self"
label var el_p12 "demand elast of public, 1% price change in self"
label var el_p13 "demand elast of private, 1% price change in self"
label var el_p14 "demand elast of doctor, 1% price change in self"

label var el_pc111 "demand elast self, 1% pricechange in self, quantile 1"
label var el_pc121 "demand elast public, 1% pricechange in self, quantile 1"

label var el_p11m "demand elast of self given 1% pricechange in self, male"
label var el_p12m "demand elast of public given 1% pricechange in self, male"

label var el_p11f "demand elast of self given 1% pricechange in self, female"
label var el_p12f "demand elast of public given 1% pricechange in self, female"

label var del_p11 "difference in elasticities by gender"

* the bootstrapped standard error of the elasticity E is the standard deviation of the empiriical distribution of elasticity E calculated by bootstrapping the sample. 
sum el_p1*
sum el_p2*
sum el_p3* 
sum el_p4*

* the confidence interval of the gender difference in elasticities is 1.96*standard deviation of the empirical distribution of difference calculated by bootstrapping the sample. 
sum del_p1*
sum del_p2*
sum del_p3* 
sum del_p4*

*If female have a lower elasticity (more negative), the difference r(mu2female)-r(mu1male) should be negative. 1-pr(negative)
* The number of times out of 200 samples when females have a more negative elasticities is ...
count if del_p11 <0   /*---> the probability of observing female ealsticities being more negative than males one is this number divided by 200   */
count if del_p22 <0
count if del_p33 <0
count if del_p44 <0




